package com.unbox.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unbox.admin.entity.RoleMenu;
import com.unbox.admin.service.IRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.unbox.admin.entity.Menu;
import com.unbox.admin.mapper.MenuMapper;
import com.unbox.admin.service.IMenuService;
import com.unbox.common.table.UnBoxPage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * <p>
 * <菜单管理业务实现类
 * </p>
 *
 * @author 王振宇
 * @since 2021-03-04
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    IRoleMenuService roleMenuService;

    @Override
    public Menu get(Menu menu) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(menu.getMenuId())) {
            lambdaQueryWrapper.eq(Menu::getMenuId, menu.getMenuId());
        }
        if (!ObjectUtils.isEmpty(menu.getAuth())) {
            lambdaQueryWrapper.eq(Menu::getAuth, menu.getAuth());
        }
        if (!StringUtils.isEmpty(menu.getComponent())) {
            lambdaQueryWrapper.eq(Menu::getComponent, menu.getComponent());
        }
        if (!ObjectUtils.isEmpty(menu.getCreateTime())) {
            lambdaQueryWrapper.eq(Menu::getCreateTime, menu.getCreateTime());
        }
        if (!ObjectUtils.isEmpty(menu.getHidden())) {
            lambdaQueryWrapper.eq(Menu::getHidden, menu.getHidden());
        }
        if (!StringUtils.isEmpty(menu.getIcon())) {
            lambdaQueryWrapper.eq(Menu::getIcon, menu.getIcon());
        }
        if (!StringUtils.isEmpty(menu.getPath())) {
            lambdaQueryWrapper.eq(Menu::getPath, menu.getPath());
        }
        if (!ObjectUtils.isEmpty(menu.getPid())) {
            lambdaQueryWrapper.eq(Menu::getPid, menu.getPid());
        }
        if (!ObjectUtils.isEmpty(menu.getQuth())) {
            lambdaQueryWrapper.eq(Menu::getQuth, menu.getQuth());
        }
        if (!StringUtils.isEmpty(menu.getTitle())) {
            lambdaQueryWrapper.eq(Menu::getTitle, menu.getTitle());
        }
        if (!ObjectUtils.isEmpty(menu.getType())) {
            lambdaQueryWrapper.eq(Menu::getType, menu.getType());
        }
        if (!StringUtils.isEmpty(menu.getName())) {
            lambdaQueryWrapper.eq(Menu::getName, menu.getName());
        }
       return super.getOne(lambdaQueryWrapper);
    }

    @Override
    public List<Menu> getList(Menu menu) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(menu.getMenuId())) {
            lambdaQueryWrapper.eq(Menu::getMenuId, menu.getMenuId());
        }
        if (!ObjectUtils.isEmpty(menu.getAuth())) {
            lambdaQueryWrapper.eq(Menu::getAuth, menu.getAuth());
        }
        if (!StringUtils.isEmpty(menu.getComponent())) {
            lambdaQueryWrapper.eq(Menu::getComponent, menu.getComponent());
        }
        if (!ObjectUtils.isEmpty(menu.getCreateTime())) {
            lambdaQueryWrapper.eq(Menu::getCreateTime, menu.getCreateTime());
        }
        if (!ObjectUtils.isEmpty(menu.getHidden())) {
            lambdaQueryWrapper.eq(Menu::getHidden, menu.getHidden());
        }
        if (!StringUtils.isEmpty(menu.getIcon())) {
            lambdaQueryWrapper.eq(Menu::getIcon, menu.getIcon());
        }
        if (!StringUtils.isEmpty(menu.getPath())) {
            lambdaQueryWrapper.eq(Menu::getPath, menu.getPath());
        }
        if (!ObjectUtils.isEmpty(menu.getPid())) {
            lambdaQueryWrapper.eq(Menu::getPid, menu.getPid());
        }
        if (!ObjectUtils.isEmpty(menu.getQuth())) {
            lambdaQueryWrapper.eq(Menu::getQuth, menu.getQuth());
        }
        if (!StringUtils.isEmpty(menu.getTitle())) {
            lambdaQueryWrapper.like(Menu::getTitle, menu.getTitle());
        }
        if (!ObjectUtils.isEmpty(menu.getType())) {
            lambdaQueryWrapper.eq(Menu::getType, menu.getType());
        }
        if (!StringUtils.isEmpty(menu.getName())) {
            lambdaQueryWrapper.eq(Menu::getName, menu.getName());
        }
        return super.list(lambdaQueryWrapper);
    }

    @Override
    public UnBoxPage<List<Menu>> getPageList(Menu menu, int pageNo, int pageSize) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(menu.getMenuId())) {
            lambdaQueryWrapper.eq(Menu::getMenuId, menu.getMenuId());
        }
        if (!ObjectUtils.isEmpty(menu.getAuth())) {
            lambdaQueryWrapper.eq(Menu::getAuth, menu.getAuth());
        }
        if (!StringUtils.isEmpty(menu.getComponent())) {
            lambdaQueryWrapper.eq(Menu::getComponent, menu.getComponent());
        }
        if (!ObjectUtils.isEmpty(menu.getCreateTime())) {
            lambdaQueryWrapper.eq(Menu::getCreateTime, menu.getCreateTime());
        }
        if (!ObjectUtils.isEmpty(menu.getHidden())) {
            lambdaQueryWrapper.eq(Menu::getHidden, menu.getHidden());
        }
        if (!StringUtils.isEmpty(menu.getIcon())) {
            lambdaQueryWrapper.eq(Menu::getIcon, menu.getIcon());
        }
        if (!StringUtils.isEmpty(menu.getPath())) {
            lambdaQueryWrapper.eq(Menu::getPath, menu.getPath());
        }
        if (!ObjectUtils.isEmpty(menu.getPid())) {
            lambdaQueryWrapper.eq(Menu::getPid, menu.getPid());
        }
        if (!ObjectUtils.isEmpty(menu.getQuth())) {
            lambdaQueryWrapper.eq(Menu::getQuth, menu.getQuth());
        }
        if (!StringUtils.isEmpty(menu.getTitle())) {
            lambdaQueryWrapper.like(Menu::getTitle, menu.getTitle());
        }
        if (!ObjectUtils.isEmpty(menu.getType())) {
            lambdaQueryWrapper.eq(Menu::getType, menu.getType());
        }
        if (!StringUtils.isEmpty(menu.getName())) {
            lambdaQueryWrapper.eq(Menu::getName, menu.getName());
        }
        lambdaQueryWrapper.orderByDesc(Menu::getCreateTime);
        Page<Menu> page = new Page<>(pageNo,pageSize);
        IPage<Menu> pageResult = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        UnBoxPage<List<Menu>> unBoxPage = new UnBoxPage<>();
        unBoxPage.setPageNo(pageNo);
        unBoxPage.setPageSize(pageSize);
        unBoxPage.setSumPageCount(pageResult.getPages());
        unBoxPage.setSumDataCount(pageResult.getTotal());
        unBoxPage.setDatas(pageResult.getRecords());
        return unBoxPage;
    }

    @Override
    public List<Menu> getIdsList(List<Integer> ids) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Menu::getMenuId, ids);
        return super.list(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean save(Menu entity) {
        return super.save(entity);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveBatch(List<Menu> entityList) {
        return super.saveBatch(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean update(Menu menu) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Menu::getMenuId, menu.getMenuId());
        return super.update(menu, lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateBatchById(List<Menu> entityList) {
        return super.updateBatchById(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean remove(Menu menu) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Menu::getMenuId, menu.getMenuId());

        // 删除关联子菜单
        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        menuLambdaQueryWrapper.eq(Menu::getPid, menu.getMenuId());

        // 删除所有角色关联菜单
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.eq(RoleMenu::getMenuId, menu.getMenuId());
        roleMenuService.remove(roleMenuLambdaQueryWrapper);

        super.remove(menuLambdaQueryWrapper);
        return super.remove(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean removeByIds(List<Integer> idList)
    {
        // 删除子菜单
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Menu::getPid, idList);

        // 删除所有角色关联菜单
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.in(RoleMenu::getMenuId, idList);
        roleMenuService.remove(roleMenuLambdaQueryWrapper);

        super.remove(lambdaQueryWrapper);
        return super.removeByIds(idList);
    }
}
